package com.alawn.framework.core.web.faces.controller;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
@Controller
public class ErrorController extends AbstractErrorController {

	public ErrorController(ErrorAttributes errorAttributes) {
		super(new DefaultErrorAttributes());
	}

	@Override
	@RequestMapping("/error")
	public String getErrorPath() {
		System.out.println("error page");
		return "404";
	}
	


	@ExceptionHandler({ UnauthorizedException.class })
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("exception", e);
		mv.setViewName("access");
		return mv;
	}

	@ResponseStatus(HttpStatus.NOT_FOUND)
	public String e404() {
		return "404";
	}

	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public String e500() {
		return "error";
	}
	
	

}
